#!/bin/sh

do_prune()
{
  PRUNEFILE="/usr/local/share/pcbsd/conf/prune-port-files"
  if [ ! -e "$PRUNEFILE" ] ; then return; fi
        
  while read pLine
  do
     if [ -z "${pLine}" ] ; then continue ; fi
     echo $pLine | grep -q "^#"
     if [ $? -eq 0 ] ; then continue ; fi

     ls ${pLine} >/dev/null 2>/dev/null
     if [ $? -eq 0 ] ; then
        echo "Pruning: $pLine"
        rm ${pLine}
     fi
  done <${PRUNEFILE}
}

do_ports_overlay()
{

  distFile="/usr/local/share/pcbsd/distfiles/port-overlay.txz"

  # Cleanup any extra port cruft that doesn't place nice with PC-BSD
  do_prune

  echo -e "Extracting ports overlay data...\c"
  cd /
  tar xvmf ${distFile} 2>/dev/null
  if [ $? -eq 0 ] ; then
    echo -e "DONE"
    exit 0
  else
    echo -e "FAILED"
    exit 1
  fi
}

do_desktop_overlay()
{
  distFile="/usr/local/share/pcbsd/distfiles/desktop-overlay.txz"
  if [ "$sysinit" != "YES" ] ; then
    excludeOpt="-X /usr/local/share/pcbsd/conf/desktop-excludes"
  fi

  echo -e "Extracting desktop overlay data...\c"
  cd /
  tar xvmf ${distFile} ${excludeOpt} 2>/dev/null
  if [ $? -eq 0 ] ; then
    echo -e "DONE"
  else
    echo -e "FAILED"
    status="1"
  fi
  post_extract_setup
}

do_server_overlay()
{
  distFile="/usr/local/share/pcbsd/distfiles/server-overlay.txz"
  if [ "$sysinit" != "YES" ] ; then
    excludeOpt="-X /usr/local/share/pcbsd/conf/server-excludes"
  fi

  echo -e "Extracting server overlay data...\c"
  cd /
  tar xvmf ${distFile} ${excludeOpt} 2>/dev/null
  if [ $? -eq 0 ] ; then
    echo -e "DONE"
  else
    echo -e "FAILED"
    status="1"
  fi
  post_extract_setup
}


# Files we need to touch or change after an upgrade
post_extract_setup()
{
  # Make sure rc.conf.pcbsd is enabled
  grep -q 'rc.conf.pcbsd' /etc/defaults/rc.conf
  if [ $? -ne 0 ] ; then
    # Setup /etc/defaults/rc.conf
    sed -i '' 's|/etc/rc.conf /etc/rc.conf.local|/etc/rc.conf.pcbsd /etc/rc.conf /etc/rc.conf.local|g' /etc/defaults/rc.conf 2>/dev/null
    grep -q "rc.conf.pcbsd" /etc/defaults/rc.conf
    if [ $? -ne 0 ] ; then
      echo "WARNING: rc.conf.pcbsd not enabled in /etc/defaults/rc.conf"
      status="1"
    fi
  fi

  # Make sure loader.conf.pcbsd is enabled
  grep -q 'loader.conf.pcbsd' /boot/defaults/loader.conf
  if [ $? -ne 0 ] ; then
    # Setup /boot/defaults/loader.conf
    sed -i '' 's|/boot/loader.conf /boot/loader.conf.local|/boot/loader.conf.pcbsd /boot/loader.conf /boot/loader.conf.local|g' /boot/defaults/loader.conf 2>/dev/null
    grep -q "loader.conf.pcbsd" /boot/defaults/loader.conf
    if [ $? -ne 0 ] ; then
      echo "WARNING: loader.conf.pcbsd not enabled in /boot/defaults/loader.conf"
      status="1"
    fi
  fi

  # See if we need to adjust pkg.conf
  grep -q "ARCH" /usr/local/etc/pkg.conf
  if [ $? -eq 0 ] ; then
    ARCH=`uname -m`
    FBSDVER=`uname -r`
    sed -i '' "s|VERSION|$FBSDVER|g" /usr/local/etc/pkg.conf
    if [ $? -ne 0 ] ; then
       echo "WARNING: Failed setting VERSION in pkg.conf"
       status="1"
    fi
    sed -i '' "s|ARCH|$ARCH|g" /usr/local/etc/pkg.conf
    if [ $? -ne 0 ] ; then
       echo "WARNING: Failed setting ARCH in pkg.conf"
       status="1"
    fi
  fi
}

show_opts()
{
   cat << EOF

usage: `basename $0` [options]
                
Options:
  ports          -- Extract port overlay files
  desktop        -- Extract desktop overlay files (Excluding user-configurable)
  server         -- Extract server overlay files (Excluding user-configurable)
  --sysinit      -- Overwrite user-configurable files (Used for first time init)

EOF

}

# Set some defaults
sysinit="no"
status="0"

# Parse CLI flags
while [ $# -gt 0 ]; do
   case "$1" in
     desktop) target="desktop" ;;
      server) target="server" ;;
       ports) target="ports" ;;
   --sysinit) sysinit="YES" ;;
           *) echo "Invalid option $1 specified!"
	      show_opts
	      exit 1
              ;;
    esac
    shift
done

# Now DO it
case $target in
    desktop) do_desktop_overlay ;;
     server) do_server_overlay ;;
      ports) do_ports_overlay ;;
          *) show_opts ;;
esac

exit $status
